package com.beordie.service.impl;

import com.beordie.dao.*;
import com.beordie.dao.impl.*;
import com.beordie.model.*;
import com.beordie.service.INoticeService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Classname NoticeServiceImpl
 * @Description TODO
 * @Date 2021/7/14 11:39
 * @Created 30500
 */
public class NoticeServiceImpl implements INoticeService {
    INoticeDao noticeDao = new NoticeDaoImpl();
    IPoliceDao policeDao = new PoliceDaoImpl();
    IDriverDao driverDao = new DriverDaoImpl();
    ICarDao carDao = new CarDaoImpl();
    IPunishmentDao punishmentDao = new PunishmentDaoImpl();
    IMiddleDao middleDao = new MiddleDaoImpl();

    @Override
    public List<Notice> getAll() {
        return null;
    }

    @Override
    public List<Notice> getAllByLimit(boolean isLimit, int start, int size) {
        return null;
    }

    @Override
    public int insert(Notice notice, String driver, String car, String police, String[] punishment) {
        Driver Did = driverDao.findByNumName(driver);
        Car Cid = carDao.getCarByNumber(car);
        Police Pid = policeDao.findByNumName(police);
        if (Did == null || Cid == null || Pid == null)
            return 0;
        notice.setCar(Cid);
        notice.setPolice(Pid);
        notice.setDriver(Did);
        int result = noticeDao.insert(notice);
        if (result < 1)
            return 0;
        int[] params = new int[punishment.length];
        for (int i = 0; i < punishment.length; i++) {
            params[i] = Integer.parseInt(punishment[i]);
            System.out.println("插入的惩罚" + params[i]);
        }

        int[] results = middleDao.insert(result, params);
        if (results == null)
            return 0;
        return 1;
    }

    @Override
    public int deleteById(int id) {
        return noticeDao.deleteById(id);
    }

    @Override
    public int updateById(Notice notice, String driver, String car, String police, String[] punishment, int id) {
        Driver Did = driverDao.findByNumName(driver);
        Car Cid = carDao.getCarByNumber(car);
        Police Pid = policeDao.findByNumName(police);
        if (Did == null || Cid == null || Pid == null)
            return 0;
        notice.setCar(Cid);
        notice.setPolice(Pid);
        notice.setDriver(Did);

        int result = noticeDao.updateById(notice, id);
        if (result < 1)
            return 0;
        int[] params = new int[punishment.length];
        for (int i = 0; i < punishment.length; i++) {
            params[i] = Integer.parseInt(punishment[i]);
            System.out.println("更新的惩罚  " + params[i]);
        }
        result = middleDao.delete(id);
        int[] results = middleDao.insert(id, params);
        if (results == null)
            return 0;
        return 1;
    }

    @Override
    public Notice findByNumName(String condition) {
        return null;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public FormatNotice getByKey(String number, String way) {
        Notice notice = noticeDao.getByKey(number, way);
        if (notice != null)
            return new FormatNotice(notice);
        else
            return null;
    }
    @Override
    public FormatNotice findByNumber(String condition){
        Notice notice = noticeDao.findByNumber(condition);
        if (notice != null)
            return new FormatNotice(notice);
        else
            return null;
    }
}
